﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebMvc.App.Web.Default.postgresqlStruct
{
    public class insertAction  : Controller
	{
		public insertAction(System.IO.TextWriter tw):base(tw){}
		public insertAction(string fileName) : base(fileName) {}
		public insertAction(HttpContext context):base(context){}
		public insertAction(Microsoft.Owin.IOwinContext context):base(context){}
        

        public void insert(string name,string pwd)
        {
            var con = new Npgsql.NpgsqlConnection(NFinal.Config.ConfigurationManager.ConnectionStrings["PostgreSql"].ConnectionString);
			con.Open();
            #region	var id; 插入并返回ID
			var __insert_id_command__ = new Npgsql.NpgsqlCommand("insert into users(name,pwd) values(@name,@pwd)", con);
			var __insert_id_parameters__=new Npgsql.NpgsqlParameter[2];
			__insert_id_parameters__[0] = new Npgsql.NpgsqlParameter("@name",NpgsqlTypes.NpgsqlDbType.Text);
			__insert_id_parameters__[0].Value = name;
			__insert_id_parameters__[1] = new Npgsql.NpgsqlParameter("@pwd",NpgsqlTypes.NpgsqlDbType.Text);
			__insert_id_parameters__[1].Value = pwd;
			__insert_id_command__.Parameters.AddRange(__insert_id_parameters__);
			var __insert_id_NonQuery=0;
				var id=0;
			try
			{
				__insert_id_NonQuery = __insert_id_command__.ExecuteNonQuery();
				__insert_id_command__.Dispose();
				if(__insert_id_NonQuery > 0)
				{
					__insert_id_command__ = new Npgsql.NpgsqlCommand("select currval(pg_get_serial_sequence('users', 'id'))", con);
					id = Convert.ToInt32(__insert_id_command__.ExecuteScalar());
					__insert_id_command__.Dispose();
				}
			}
			catch
			{
				__insert_id_command__.Dispose();
			}
			#endregion
			
            con.Close();
            Write(id);
        }
        
        
        
        
        
        
        
    }
}